﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using AppCommon;
using Controller;
using Model;

public partial class TourCategory : System.Web.UI.Page
{
    public static string pageUrl;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            var id = Request.QueryString["UniqueId"];
            var page = Request.QueryString["Page"];
            if (id != null)
            {
                var pageNumer = 1;
                if (!string.IsNullOrEmpty(page))
                {                    
                    try
                    {
                        pageNumer = Convert.ToInt32(page);
                    }
                    catch (Exception)
                    {
                        Response.Redirect("~/404.aspx");
                        throw;
                    }
                }
                litTitle.Text = id;
                if (pageNumer == 0)
                    pageNumer += 1;
                FillData(id, pageNumer);
            }
        }
    }

    public void FillData(string uniqueid, int currentPage)
    {
        var id = GetIdLoaitour(uniqueid);
        var nextPage = 0;
        var prePage = 0;
        
        // get list tour follow id loai tour
        var controller = new VYC01Controller();
        var model = new List<ModelVYC01>();
        model = uniqueid.Equals(AppCommon.Common.ConvertToUnSign(Constants.TITLE_TOUR_DOCDAO)) ? controller.GetTourDocDao(1) : controller.GetTheoLoaiTour(id, 1);
        if (model.Count > 0)
        {
            litnumerCurrent.Text = currentPage.ToString();
            var pagedDataSource = new PagedDataSource();

            pagedDataSource.DataSource = model;
            pagedDataSource.AllowPaging = true;
            pagedDataSource.PageSize = 8;
            pagedDataSource.CurrentPageIndex = currentPage - 1;

            var totalPage = pagedDataSource.PageCount; // tong so trang   

            if (currentPage <= 1 && totalPage == 1)
            {
                nextPage = currentPage;
                prePage = currentPage;
            }
            else
            {
                nextPage = currentPage + 1;
                prePage = currentPage - 1;
            }

            litnumberTotal.Text = totalPage.ToString();

            linkPageFirst.NavigateUrl = pageUrl + "/";
            linkPageNext.NavigateUrl = pageUrl + "/" + nextPage;
            linkPagePre.NavigateUrl = pageUrl + "/" + prePage;

            linkPageLast.NavigateUrl = pageUrl + "/" + totalPage;

            repeateListtour.DataSource = pagedDataSource;
            repeateListtour.DataBind();
        }
    }

    public int GetIdLoaitour(string uniqueid)
    {
        var controller = new VYC02Controller();
        var model = controller.GetByUniqueId(uniqueid);
        litTitle.Text = model[0].Ten;
        litTenTour.Text = model[0].Ten;
        pageUrl = "~/category/" + model[0].UniqueId;
        return model.Count > 0 ? model[0].Id : 0;
    }

    public string GetListNgaykhoihanh(string idTour)
    {
        var sb = new StringBuilder();        
        var controller = new VYC0101Controller();
        var model = controller.GetByIdTour(idTour, 1);
        if (model.Count > 0)
        {
            foreach (var modelVyc0101 in model)
            {
                sb.Append(modelVyc0101.NgayGioDi.ToString("dd/MM/yyyy"));
                sb.Append(", ");
            }
        }
        return sb.ToString();
    }
}